﻿using Microsoft.AspNetCore.Diagnostics;
using Microsoft.AspNetCore.Http;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TESTCommons.HttpHelper;
using TESTCommons.JsonHelper;
using TestManage.Infrastructure.Extensions;
using TestModel.Configuration;
using TestModel.SysManage;

namespace TestManage.Infrastructure.LogWrite
{
    public static class LogHelper
    {
        public static Task ErrorEvent(HttpContext context)
        {
            LogHelper.WriteLog(context);
            return context.Response.WriteAsync(JsonSerialize.SerializeObject(new AjaxResult { type = ResultType.error, errorcode = 444, message = "" }), Encoding.GetEncoding("UTF-8"));
        }

        public static void WriteLog(HttpContext context)
        {
            var feature = context.Features.Get<IExceptionHandlerFeature>();
            var ex = feature?.Error;

            sys_Log model = new sys_Log();
            model.gid = new Guid();
            model.PageUrl = context.Request.Path;
            model.ShortMessage = ex.Message;
            model.FullMessage = "错误源:" + ex.Source + "\r\n堆栈信息:" + ex.StackTrace;

            //var congfig = EngineContext.Current.Resolve<TestConfig>();
            var result = WebHelper.PostAsync<AjaxResult>(context.Request.Scheme + "//" + context.Request.Host.Value + "/api/SysLog/AddLog", model);
            Console.Write(result);
        }
    }
}
